package cn.life.business.car.dao.impl;

import cn.life.business.car.dao.CarDao;
import cn.life.business.car.entity.po.Car;
import cn.life.business.car.entity.vo.CarVo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import org.springframework.stereotype.Repository;


/**
 * @author wangzhenyu
 * @Date 2018/7/6  16:48
 */
@Repository
public class CarDaoImpl extends AbstractSupportDao implements CarDao {

    @Override
    public int  addCar(Car car){
        String sql = "INSERT INTO factory_car(car_no,car_purpose,oil_code,oil_amount,applicant_name,file_url,money," +
                "project_id,opr_user_id,opr_user_name,opr_date) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql,car.getCarNo(),car.getCarPurpose(),car.getOilCode(),car.getOilAmount(),
                car.getApplicantName(),car.getFileUrl(),car.getMoney(),car.getProjectId(),car.getOprUserId(),
                car.getOprUserName(),car.getOprDate());
    }

    @Override
    public int deleteCar(long carId) {
        String sql= "delete from factory_car where car_id=?";
        return update(sql,carId);
    }

    @Override
    public int update(Car car) {
        String sql= "update factory_car set car_no=?,car_purpose=?," +
                "oil_code=?,oil_amount=?,file_url=?,money=? where car_id=?";
        return update(sql,car.getCarNo(),car.getCarPurpose(),car.getOilCode(),
                car.getOilAmount(),car.getFileUrl(),car.getMoney(),car.getCarId());
    }

    @Override
    public Pagination<CarVo> getAllCarInfo(CarVo carVo) {
        StringBuffer sql= new StringBuffer("select * from factory_car where 1=1");
              if ((!"".equals(carVo.getCarNo())&&carVo.getCarNo()!=null)||(!"".equals(carVo.getApplicantName())&&carVo.getApplicantName()!=null)){
            sql.append(" and (car_no like'%"+carVo.getCarNo()+"%' or applicant_name like '%"+carVo.getApplicantName()+"%') ");
        }

        if (!"".equals(carVo.getCarPurpose())&&carVo.getCarPurpose()!=null){
            sql.append(" and car_purpose like'%"+carVo.getCarPurpose()+"%' ");
        }

        if (!"".equals(carVo.getSelectDate())&&carVo.getSelectDate()!=null){
            sql.append(" and  '" +  getSqlDate(carVo.getSelectDate()) + "' <=opr_date");
        }

        if (carVo.getProjectId()!=null && !"".equals(carVo.getProjectId())){
            sql.append(" and  project_id = " + carVo.getProjectId() + " ");
        }
        sql.append("   ORDER BY opr_date DESC ");

        return queryForListByPageEx(sql.toString(),CarVo.class,carVo.getPageIndex(),carVo.getPageSize());
    }

    @Override
    public CarVo getCar(CarVo carVo) {
        String sql= "select * from factory_car where car_id=? ";
        return queryForObject(sql,CarVo.class,carVo.getCarId());
    }


}
